#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

struct ListNode {
    int val;
    struct ListNode* next;
};

struct ListNode* reverseList(struct ListNode* head) {
    if (head == NULL)
        return NULL;
    struct ListNode* n1, * n2, * n3;
    n1 = NULL;
    n2 = head;
    n3 = n2->next;

    while (n2)
    {
        n2->next = n1;

        n1 = n2;
        n2 = n3;
        if (n3 != NULL)
            n3 = n3->next;
    }

    return n1;
}

struct ListNode* reverseList(struct ListNode* head) {
    struct ListNode* cur = head;
    struct ListNode* first = head;
    struct ListNode* newnode = NULL;

    while (cur)
    {
        cur = cur->next;

        first->next = newnode;
        newnode = first;

        first = cur;
    }

    return newnode;
}

struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    struct ListNode* guard = NULL, * tail = NULL;
    guard = tail = (struct ListNode*)malloc(sizeof(struct ListNode));
    tail->next = NULL;
    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;

    while (cur1 && cur2)
    {
        if (cur1->val < cur2->val)
        {
            tail->next = cur1;
            tail = tail->next;
            cur1 = cur1->next;
        }
        else
        {
            tail->next = cur2;
            tail = tail->next;
            cur2 = cur2->next;
        }
    }

    if (cur1)
        tail->next = cur1;

    if (cur2)
        tail->next = cur2;


    return guard->next;
}


struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
    struct ListNode* head, * tail;
    head = tail = NULL;

    if (list1 == NULL)
        return list2;

    if (list2 == NULL)
        return list1;

    struct ListNode* cur1 = list1;
    struct ListNode* cur2 = list2;

    while (cur1 && cur2)
    {
        if (cur1->val < cur2->val)
        {
            if (head == NULL)
            {
                head = tail = cur1;
            }
            else
            {
                tail->next = cur1;
                tail = tail->next;
            }

            cur1 = cur1->next;
        }
        else
        {
            if (head == NULL)
            {
                head = tail = cur2;
            }
            else
            {
                tail->next = cur2;
                tail = tail->next;
            }

            cur2 = cur2->next;
        }
    }

    if (cur1)
        tail->next = cur1;

    if (cur2)
        tail->next = cur2;


    return head;
}

struct ListNode* partition(struct ListNode* pHead, int x) {
    // write code here
    struct ListNode* greaterGuard, * Gtail, * Ltail, * lowerGuard;
    greaterGuard = Gtail = (struct ListNode*)malloc(sizeof(struct ListNode));
    lowerGuard = Ltail = (struct ListNode*)malloc(sizeof(struct ListNode));
    Gtail->next = Ltail->next = NULL;

    struct ListNode* cur = pHead;
    while (cur)
    {
        if (cur->val < x)
        {
            Ltail->next = cur;
            Ltail = Ltail->next;
        }
        else
        {
            Gtail->next = cur;
            Gtail = Gtail->next;
        }
        cur = cur->next;
    }
    Gtail->next = NULL;
    Ltail->next = greaterGuard->next;
    free(greaterGuard);
    greaterGuard = NULL;

    return lowerGuard->next;
}
